package com.feinno.urcs.group.http.handler;

import com.feinno.urcs.group.GroupError;
import com.feinno.urcs.group.GroupException;
import com.feinno.urcs.group.entity.GroupIndex;
import com.feinno.urcs.group.http.servlet.BaseHttpServlet;
import com.feinno.urcs.group.http.servlet.GroupHttpContext;
import com.feinno.urcs.group.http.util.HttpHelper;
import com.feinno.urcs.types.MobileNo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * Created by wuzhiguo on 15-5-14.
 */
public class RemoveGroupMemberHandler extends BaseHttpServlet {

    private static final Logger LOGGER = LoggerFactory.getLogger(RemoveGroupMemberHandler.class);

    @Override
    public void process(final GroupHttpContext ctx) throws IOException {
        try {
            if (LOGGER.isInfoEnabled()) {
                String requestString = HttpHelper.getRequestString(ctx.getRequest(), ctx.getBodyString());
                LOGGER.info(String.format("Received Request: %s", requestString));
            }

            if (ctx.getTargetIMPU() == null) {
                String requestString = HttpHelper.getRequestString(ctx.getRequest(), ctx.getBodyString());
                LOGGER.error(String.format("targetIMPU is null. Received Request: %s", requestString));
                ctx.getResponse().sendError(400, "ArgsError");
                return;
            }

            // 调用GroupService
//            GroupIndex groupIndex = new GroupIndex(getGroupIdFromUri(ctx.getXuiIMPU().toString()), 0);
            int groupId = getGroupIdFromUri(ctx.getXuiIMPU().toString());
            GroupIndex groupIndex = ctx.getGroupService().getGroupIndex(groupId);
            if (ctx.getSenderIMPU().equals(ctx.getTargetIMPU())) {
                ctx.getGroupService().quitGroup(groupIndex,  MobileNo.parseFrom(ctx.getSenderIMPU().getUser()));
            } else {
                ctx.getGroupService().removeGroupMembers(groupIndex, MobileNo.parseFrom(ctx.getSenderIMPU().getUser()),  MobileNo.parseFrom(ctx.getTargetIMPU().getUser()));
            }
            int groupInfoVer = ctx.getGroupInfoVersionCacheManager().getCurGrpInfoVer(groupId);
            if (groupInfoVer < 0) {
                groupInfoVer = 0;
            }
            ctx.getResponse().setStatus(200);
            ctx.getResponse().setHeader("ETag", String.valueOf(groupInfoVer));
            setDigestAuthenticationInfo(ctx);
            ctx.getResponse().setContentLength(0);

            if (LOGGER.isInfoEnabled()) {
                String requestString = HttpHelper.getRequestString(ctx.getRequest(), ctx.getBodyString());
                LOGGER.info(String.format("process() Successfully. Received Request: %s", requestString));
            }

        } catch (GroupException gex) {
            GroupError error = gex.getError();
            String requestString = HttpHelper.getRequestString(ctx.getRequest(), ctx.getBodyString());
            LOGGER.error(String.format("process() GroupException. GroupError Code: %s Received Request: %s",
                    error.getSipCode(), requestString), gex);
            ctx.getResponse().sendError(error.getSipCode(), "GroupError");
        } catch (Exception ex) {
            String requestString = HttpHelper.getRequestString(ctx.getRequest(), ctx.getBodyString());
            LOGGER.error(String.format("process() Error. Received Request: %s", requestString), ex);
            ctx.getResponse().sendError(500, "ServerError");
        }
    }
}
